package main

import (
	"github.com/astaxie/beego/logs"
	"github.com/gorilla/websocket"
	"net/http"
	"time"
)

//10.201.16.46:59335/ledata
func main() {
	var wsurl = "ws://192.168.3.38:56080/v1/GPSLocation"
	var origin = "http://192.168.3.38:56080/v1/GPSLocation"
	request, err := http.NewRequest("get", origin, nil)
	if err != nil {
		logs.Error(err)
		return
	}
	ws, _, err := websocket.DefaultDialer.Dial(wsurl, request.Header)
	if err != nil {
		panic(err)
	}
	//time.Sleep(6 * time.Second)
	//time.Sleep(5*time.Second)
	ws.SetPongHandler(func(appData string) error {
		logs.Debug("pong:", appData)
		return nil
	})
	go func() {
		for {
			logs.Debug("ping")
			err := ws.WriteMessage(websocket.PingMessage, []byte("ping"))
			if err != nil {
				logs.Error(err)
			}
			time.Sleep(3 * time.Second)
		}
	}()
	//ws.SetPongHandler(func(appData string) error {
	//	_ = ws.SetReadDeadline(time.Now().Add(5 * time.Second))
	//	return nil
	//})
	for {
		_, m, err := ws.ReadMessage()
		if err != nil {
			logs.Error(err)
			break
		}
		logs.Info(string(m))
		//rex := regexp.MustCompile(`"report_at":"(.*?)"`)
		//for _, i := range rex.FindAllStringSubmatch(string(m), -1) {
		//	if len(i) >= 2 {
		//		fmt.Println("上报时间:",i[1],"本地时间:",time.Now())
		//	}
		//}
	}
}
